using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SCSEngine.Services.Input;
using Microsoft.Xna.Framework.Input.Touch;
using SCSEngine.Services;
using SCSEngine.Mathematics;
using TriUan.TriUanGame.GameComponents.Polygon;
using TriUan.TriUanGame.GameComponents.Polygon.Implements;
using TriUan.TriUanGame.GameComponents;
using TriUan.TriUanGame;
using SVLvsXNA;

namespace TriUan.TriUanGameComponent.GameComponents
{
    public class Piece : TUDrawableGameComponent
    {
        public static Piece IsMoving = null;

        private SCSServices services;
        private SpriteBatch spriteBatch;
        private SpriteFont font;

        private Texture2D image;
        public Vector2 Position
        {
            get { return this.PieceInfo.Position; }
            set
            {
                this.PieceInfo.Position = value;
            }
        }

        public float Angle
        {
            get { return this.PieceInfo.Rotation; }
            private set
            {
                this.PieceInfo.Rotation = value;
            }
        }

        public Vector2 Center { get; set; }

        private SpriteEffects effect;
        public SpriteEffects Effect
        {
            get { return this.effect; }
            set
            {
                this.effect = value;
            }
        }
        public Vector2 Origin
        {
            get
            {
                return this.Vertices.Origin;
            }
        }

        public float Scale
        {
            get
            {
                return this.PieceInfo.Scale;
            }
        }
        private InputHandle inputHandle;

        private bool enable = false;

        public ITUPolygon Vertices;
        private ITUPolygon copyVertices;
        public ITUPieceInfo PieceInfo;

        private PiecesManager _picesMgr;
        public ITUPolygon TransformVertices
        {
            get
            {
                for (int i = 0; i < Vertices.Vertices.Count; i++)
                {
                    copyVertices.Vertices[i] = Vertices.Vertices[i];
                    copyVertices.Vertices[i] += this.Position - Vertices.Origin;
                    copyVertices.Vertices[i] = GMath.Transform(copyVertices.Vertices[i], this.Position, this.Angle);
                }
                return copyVertices;
            }
        }
        private Vector2 selectionPoint;
        Texture2D point;

        public Piece(GamePage gamePage, PiecesManager picesMgr, ITUPolygon vertices, ITUPieceInfo piceInfo, Texture2D image)
            : base(gamePage)
        {
            _picesMgr = picesMgr;
            // Load from XML - Data test
            this.Vertices = vertices;
            this.PieceInfo = piceInfo;
            this.copyVertices = new TUPolygon(new List<Vector2>(vertices.Vertices), vertices.Origin);
            Center = new Vector2(image.Width / 2 + vertices.Origin.X, image.Height / 2 + vertices.Origin.Y);

            this.image = image;
            // Initialized piece position via started
            this.selectionPoint = this.Position;

            this.effect = SpriteEffects.None;

            // Get some services
            services = (SCSServices)gamePage.Services.GetService(typeof(SCSServices));
            spriteBatch = services.SpriteBatch;
            inputHandle = services.InputHandle;

            font = gamePage.Content.Load<SpriteFont>("DebugFont");
            point = gamePage.Content.Load<Texture2D>("point");
        }

        public override void Update(GameTime gameTime)
        {
            for (int i = 0; i < Vertices.Vertices.Count; i++)
            {
                copyVertices.Vertices[i] = Vertices.Vertices[i];
                copyVertices.Vertices[i] += this.Position - Vertices.Origin;
                copyVertices.Vertices[i] = GMath.Transform(copyVertices.Vertices[i], this.Position, this.Angle);
            }

            foreach (GestureSample sample in inputHandle.Gestures)
            {
                List<Vector2> list = copyVertices.Vertices;

                if (sample.GestureType == GestureType.Tap)
                {
                    this.Angle += (float)Math.PI / 4;
                }

                if (sample.GestureType == GestureType.Hold)
                {
                    this.Effect = this.Effect == SpriteEffects.None ? SpriteEffects.FlipHorizontally : SpriteEffects.None;

                    Vector2 copyOrigin = Vertices.Origin;
                    copyOrigin.X *= -1;
                    Vertices.Origin = copyOrigin;

                    Center = new Vector2(image.Width / 2 + Vertices.Origin.X, image.Height / 2 + Vertices.Origin.Y);

                    for (int i = 0; i < Vertices.Vertices.Count; i++)
                    {
                        Vector2 vec = Vertices.Vertices[i];
                        vec.X *= -1;
                        Vertices.Vertices[i] = vec;
                    }
                }
                else if ((sample.GestureType == GestureType.FreeDrag && sample.GestureType != GestureType.Flick) || _picesMgr.LastPiece == this)
                {
                    this.Position += sample.Delta;
                }
                

            }



            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            foreach (var item in TransformVertices.Vertices)
            {
                //spriteBatch.Draw(point, item, null, Color.Black, 0f, new Vector2(point.Width / 2f, point.Height / 2f), 1f, SpriteEffects.None, 1f);
            }
            spriteBatch.Draw(this.image, this.Position, null, Color.White, this.Angle, Center, this.Scale, effect, 1f);
            //spriteBatch.DrawString(font, this.enable.ToString(), Vector2.Zero, Color.Black);
            //spriteBatch.DrawString(font, this.Position.X + ", " + this.Position.Y, Position, Color.Black);
            //spriteBatch.DrawString(font, "SelectionPoint: " + selectionPoint.X + ", " + selectionPoint.Y, new Vector2(0, 60), Color.Black);
            //if (Piece.IsMoving == null)
            //    spriteBatch.DrawString(font, "NULL: ", new Vector2(300, 60), Color.Black);



            //spriteBatch.Draw(point, PieceInfo.Position, Color.Yellow);

            base.Draw(gameTime);
        }

    }
}
